package com.recipes.app;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.SQLException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class RecipesList extends Activity {

	ImageView imgSearchNav;
	LinearLayout imgBack;
	LinearLayout home;
	LinearLayout layoutNav;
	Button btnSearch;
	EditText edtSearch;
	LinearLayout lytSearchForm;
	ListView listRecipes;
	ProgressBar prgLoading;
	TextView txtAlert, txtTitle;

	String RecipeNameKeyword = "";

	static DBHelper dbhelper;
	ArrayList<ArrayList<Object>> data;
	ListAdapter la;

	static int[] id;
	static String[] RecipeName;
	static String[] Preview;
	static String[] CookTime;

	private int cateId;

	private static String SDPATH = "/mnt/sdcard";
	static String title;

	/** This class is used to create custom listview */
	static class ListAdapter extends BaseAdapter {
		private LayoutInflater inflater;
		private Context ctx;

		public ListAdapter(Context context) {
			inflater = LayoutInflater.from(context);
			ctx = context;
		}

		public int getCount() {
			// TODO Auto-generated method stub
			return RecipeName.length;
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder;
			Bitmap bmpDefaultPic;

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.row, null);
				holder = new ViewHolder();
				holder.txtRecipeName = (TextView) convertView
						.findViewById(R.id.txtRecipeName);
				holder.txtReadyIn = (TextView) convertView
						.findViewById(R.id.txtReadyIn);
				holder.imgPreview = (ImageView) convertView
						.findViewById(R.id.imgPreview);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			String path = SDPATH + "/db_images" + "/" + Preview[position];
			bmpDefaultPic = BitmapFactory.decodeFile(path + ".jpg", null);

			holder.txtRecipeName.setText(RecipeName[position]);
			holder.txtReadyIn.setText("Cook time " + CookTime[position]);
			//
			// int imagePreview = ctx.getResources().getIdentifier(
			// Preview[position], "drawable", ctx.getPackageName());
			// holder.imgPreview.setImageResource(imagePreview);

			if (bmpDefaultPic == null) {
				holder.imgPreview.setImageResource(R.drawable.image_preview);
			} else {

				holder.imgPreview.setImageBitmap(bmpDefaultPic);
			}

			return convertView;
		}

		static class ViewHolder {
			TextView txtRecipeName, txtReadyIn;
			ImageView imgPreview;
		}

	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recipes_list);

		// SDPATH = Environment.getExternalStorageDirectory().getPath();
		Intent intent = getIntent();
		cateId = intent.getIntExtra("id_for_list", -1);
		title = intent.getStringExtra("title_for_list");
		dbhelper = new DBHelper(this);
		la = new ListAdapter(this);

		imgBack = (LinearLayout) findViewById(R.id.imgBack);
		imgSearchNav = (ImageView) findViewById(R.id.imgSearchNav);
		btnSearch = (Button) findViewById(R.id.btnSearch);
		edtSearch = (EditText) findViewById(R.id.edtSearch);
		lytSearchForm = (LinearLayout) findViewById(R.id.lytSearchForm);
		layoutNav = (LinearLayout) findViewById(R.id.layoutSearchNav);
		
		listRecipes = (ListView) findViewById(R.id.listRecipes);
		prgLoading = (ProgressBar) findViewById(R.id.prgLoading);
		txtAlert = (TextView) findViewById(R.id.txtAlert);
		txtTitle = (TextView) findViewById(R.id.txtTitleApp);
		txtTitle.setText(title);

		/**
		 * when this app's installed at the first time, code below will copy
		 * database stored in assets to /data/data/com.recipes.app/databases/
		 */
		try {
			dbhelper.createDataBase();
		} catch (IOException ioe) {
			throw new Error("Unable to create database");
		}

		/** then, the database will be open to use */
		try {
			dbhelper.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		}

		new getDataTask().execute();

		listRecipes.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub

				/**
				 * when one of item in the list is clicked, this app will access
				 * RecipeDetail.class. it also send id value to that class
				 */
				Intent i = new Intent(RecipesList.this, RecipeDetail.class);
				i.putExtra("id_for_detail", id[position]);
				i.putExtra("category_for_detail", title);
				i.putExtra("title_for_detail", RecipeName[position]);
				startActivity(i);
			}
		});

		layoutNav.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub

				/** this code is used to hide and show the search form */
				if (lytSearchForm.getVisibility() == 8) {
					lytSearchForm.setVisibility(0);
					imgSearchNav.setImageResource(R.drawable.nav_down);
				} else {
					lytSearchForm.setVisibility(8);
					imgSearchNav.setImageResource(R.drawable.nav_up);
				}
			}
		});

		btnSearch.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				RecipeNameKeyword = edtSearch.getText().toString();
				try {
					dbhelper.openDataBase();
				} catch (SQLException sqle) {
					throw sqle;
				}
				new getDataTask().execute();
			}
		});

		imgBack.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub

				finish();
			}
		});

		home = (LinearLayout) findViewById(R.id.home);

		home.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent i = new Intent(RecipesList.this, CategoryList.class);
				i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(i);
				finish();
			}
		});
	}

	/** this class is used to handle thread */
	public class getDataTask extends AsyncTask<Void, Void, Void> {

		getDataTask() {
			if (!prgLoading.isShown()) {
				prgLoading.setVisibility(0);
				txtAlert.setVisibility(8);
			}
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub

		}

		@Override
		protected Void doInBackground(Void... arg0) {
			// TODO Auto-generated method stub
			getDataFromDatabase(RecipeNameKeyword);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			prgLoading.setVisibility(8);
			if (id.length > 0) {
				listRecipes.setVisibility(0);
				listRecipes.setAdapter(la);
			} else {
				txtAlert.setVisibility(0);
			}
			dbhelper.close();
		}
	}

	/**
	 * this code is used to get data from database and store them to array
	 * attributes
	 */
	public void getDataFromDatabase(String RecipeNameKeyword) {
		data = dbhelper.getAllData(RecipeNameKeyword, cateId);

		id = new int[data.size()];
		RecipeName = new String[data.size()];
		Preview = new String[data.size()];
		CookTime = new String[data.size()];

		for (int i = 0; i < data.size(); i++) {
			ArrayList<Object> row = data.get(i);

			id[i] = Integer.parseInt(row.get(0).toString());
			RecipeName[i] = row.get(1).toString();
			Preview[i] = row.get(2).toString().trim();
			CookTime[i] = row.get(3).toString();

		}
	}

	@Override
	public void onConfigurationChanged(final Configuration newConfig) {
		// Ignore orientation change to keep activity from restarting
		super.onConfigurationChanged(newConfig);
	}

}